/**
 * author:Administrator
 * date:2016/3/15
 * desc: manager command 接口
 */

'use strict';
let FormatOutput = require('../infrastructure/appUtil/FormatOutput.js');

let api = {};
module.exports = api;

/**创建 category*/
let Category = require('../domain/model_cmd/category/Category.js');
let CateRepo = require('../domain/model_cmd/category/CategoryRepo.js');
api.createCategory = function *(name, pid) {
  let pojo = {name: name};
  if (pid) {
    let parent = yield CateRepo.find(pid);
    if (!parent) return false;
    pojo.pid = parent.id;
  }
  let cate = new Category(pojo);
  yield cate.store();
  return cate;
};

/**创建 Location*/
let Location = require('../domain/model_cmd/location/Location.js');
let LocRepo = require('../domain/model_cmd/location/LocationRepo.js');
api.createLocation = function *(name, pid) {
  let pojo = {name: name};
  if (pid) {
    let parent = yield LocRepo.find(pid);
    if (!parent) return false;
    pojo.pid = parent.id;
    //pojo.level = parent.level + 1;
  }

  let loc = new Location(pojo);
  yield loc.store();
  return loc;
};